// pages/sys/sys.js
const common = require('../../public.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
      selected:2,
    },
    onTabItemTap(e) {
        // var that = this
        // common.getLocalUserInfo(function (success, data) {
        //     if (success) {
        //         that.setData({
        //             userInfo: data.user,
        //             token: data.token
        //           }, function () {
        //             that.read_message()
        //             that.scan()
        //           }) 
        //     } else {
        //         wx.navigateTo({
        //             url: '/pages/login/index',
        //         })
        //     }
        // })


    },
      // 统计用户未读消息
      read_message() {
        var that = this
        var tmp = {
          userId: this.data.userInfo.id,
          token: this.data.token
        }
        common.get('/alipush/getUserNotReadMessageCount', tmp, res => {
          if (res.status === 200) {
            // if(res.data>0){
            //   wx.showTabBarRedDot({
            //     index: 3,
            //   }) 
            //   return
            // }
            // wx.hideTabBarRedDot({
            //   index: 3,
            // })
          } else {
            common.showRequestErrMsg(res)
          }
    
        })
      },
    scan() {
        wx.scanCode({
            success: (res) => {
                const result = res.result
                console.log(result, "1111133333333333333333")
                //1. 扫码支付
                if (result && result.indexOf('/pay/scanForPay') != -1) {
                    const quary = result.split('?')[1]
                    console.log(quary,"悟已")
                    wx.redirectTo({
                        url: '/pages/scanForPay/index?' + quary,
                    })
                } else {
                    common.showToast('暂不支持当前功能~')
                    // const quary = result.split('?')[1]
                    // wx.redirectTo({
                    //     url: '/packageC/pages/choiceMealPerson/index?' + quary,
                    // })

                }
            },
            fail: (res) => {
                if (res.errMse != 'scanCode:fail cancel') {
                    common.showToast('扫码失败')
                }
                wx.redirectTo({
                    url: '/pages/index/index',
                })

            }
        })
    },

    shua() {
        let pages = getCurrentPages();
        // console.log(pages)
        let prevpage = pages[pages.length - 2];

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this
      common.getLocalUserInfo(function (success, data) {
          if (success) {
              that.setData({
                  userInfo: data.user,
                  token: data.token
                }, function () {
                  that.read_message()
                  that.scan()
                }) 
          } else {
              wx.navigateTo({
                  url: '/pages/login/index',
              })
          }
      })

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.shua()
    },
})